﻿using Microsoft.Extensions.DependencyInjection;

namespace Athena.Transport.BeetleX
{
    using Runtime.Server;
    using Runtime.Server.Implementations;
    public static class BeetleXTransportExtension
    {
        public static IServiceCollection AddBeetleXTransport(this IServiceCollection serviceCollection)
        {
            serviceCollection.AddSingleton<ITransportClientFactory, BeetleXTransportClientFactory>();

            serviceCollection.AddSingleton<BeetleXTransportMessageListener>();
            serviceCollection.AddSingleton<ISerivceHost, ServiceHost>(provider =>
            {
                var serivceExecutor = provider.GetRequiredService<IServiceExecutor>();
                var beetleXTransportMessageListener = provider.GetRequiredService<BeetleXTransportMessageListener>();

                return new ServiceHost(async endpont =>
                {
                    await beetleXTransportMessageListener.StartAsync(endpont);
                    return beetleXTransportMessageListener;
                }, serivceExecutor);
            });

            return serviceCollection;
        }
    }
}
